/*

Replication file for Kirkland and Banda "Perceived Ideological Distance and Trust in Congress"
	To use the same plot scheme as we did, enter the following code while connected
	to the internet:
	
ssc install blindschemes, replace all
set scheme plottig

The first line of code is only needed if your version of Stata is older than
	version 15 and you don't already have the scheme installed.

*/

**********************
* 2008 CCES analysis *
**********************


use "2008 CCES replication.dta", clear


* Summary statistics
sum trustcongress2 hiddiff1 seniddiff1 seniddiff2 i.pid3 i.housedem i.sen1dem i.sen2dem ///
	female age nonwhite interest education2 nateconomy


* Direct test
meglm trustcongress2 hiddiff1 i.pid3##c.housedem female age nonwhite interest ///
	education2 nateconomy [pweight=weights] || statdist:, family(ordinal)
estat ic
margins, at(hiddiff1=(0(10)100)) predict(outcome(1))
marginsplot, recast(line) recastci(rarea) xtitle(" " "Perceived ideological distance") ///
	ytitle("Predicted probability of hardly ever trusting Congress" " ") ylabel(0(.1)1) ///
	title("U.S. House Member") plotopts(lw(.7)) ysize(4) xsize(4)

meglm trustcongress2 seniddiff1 i.pid3##c.sen1dem female age nonwhite interest ///
	education2 nateconomy [pweight=weights] || statdist:, family(ordinal)
estat ic
margins, at(seniddiff1=(0(10)100)) predict(outcome(1))
marginsplot, recast(line) recastci(rarea) xtitle(" " "Perceived ideological distance") ///
	ytitle("Predicted probability of hardly ever trusting Congress" " ") ylabel(0(.1)1) ///
	title("Senior Senator") plotopts(lw(.7)) ysize(4) xsize(4)

meglm trustcongress2 seniddiff2 i.pid3##c.sen2dem female age nonwhite interest ///
	education2 nateconomy [pweight=weights] || statdist:, family(ordinal)
estat ic
margins, at(seniddiff2=(0(10)100)) predict(outcome(1))
marginsplot, recast(line) recastci(rarea) xtitle(" " "Perceived ideological distance") ///
	ytitle("Predicted probability of hardly ever trusting Congress" " ") ylabel(0(.1)1) ///
	title("Junior Senator") plotopts(lw(.7)) ysize(4) xsize(4)


* 2008 CCES appendix material	

* Placebo test

meglm trustprez hiddiff1 i.pid3##c.housedem female age nonwhite interest ///
	education2 nateconomy [pweight=weights] || statdist:, family(ordinal)
estat ic

meglm trustprez seniddiff1 i.pid3##c.sen1dem female age nonwhite interest ///
	education2 nateconomy [pweight=weights] || statdist:, family(ordinal)
estat ic

meglm trustprez seniddiff2 i.pid3##c.sen2dem female age nonwhite interest ///
	education2 nateconomy [pweight=weights] || statdist:, family(ordinal)
estat ic	


* Copartisans and opposing partisans

* Copartisans
meglm trustcongress2 hiddiff1 i.pid3 female age nonwhite interest education2 nateconomy ///
	if (housedem==0 & pid3==3) | (housedem==1 & pid3==1) [pweight=weights] || statdist:, family(ordinal)
estat ic
	
meglm trustcongress2 seniddiff1 i.pid3 female age nonwhite interest education2 nateconomy ///
	if (sen1dem==0 & pid3==3) | (sen1dem==1 & pid3==1) [pweight=weights] || statdist:, family(ordinal)
estat ic

meglm trustcongress2 seniddiff2 i.pid3 female age nonwhite interest education2 nateconomy ///
	if (sen2dem==0 & pid3==3) | (sen2dem==1 & pid3==1) [pweight=weights] || statdist:, family(ordinal)
estat ic

* Opposing partisans
meglm trustcongress2 hiddiff1 i.pid3 female age nonwhite interest education2 nateconomy ///
	if (housedem==0 & pid3==1) | (housedem==1 & pid3==3) [pweight=weights] || statdist:, family(ordinal)
estat ic
	
meglm trustcongress2 seniddiff1 i.pid3 female age nonwhite interest education2 nateconomy ///
	if (sen1dem==0 & pid3==1) | (sen1dem==1 & pid3==3) [pweight=weights] || statdist:, family(ordinal)
estat ic
	
meglm trustcongress2 seniddiff2 i.pid3 female age nonwhite interest education2 nateconomy ///
	if (sen2dem==0 & pid3==1) | (sen2dem==1 & pid3==3) [pweight=weights] || statdist:, family(ordinal)
estat ic


**********************
* 2016 CCES analysis *
**********************


use "2016 CCES replication.dta", clear


* Summary statistics
sum trustcongress2 hiddiff1 seniddiff1 seniddiff2 i.pid3 i.housedem i.sen1dem i.sen2dem ///
	female age nonwhite interest education2 nateconomy


* Analysis

* Direct test
* In Senate models, fips and statdist as random intercepts both work
meglm trustcongress2 hiddiff1 i.pid3##c.housedem female age nonwhite interest ///
	education2 nateconomy [pweight=weights] || statdist:, family(ordinal)
estat ic

meglm trustcongress2 seniddiff1 i.pid3##c.sen1dem female age nonwhite interest ///
	education2 nateconomy [pweight=weights] || statdist:, family(ordinal)
estat ic
margins, at(seniddiff1=(0(1)6)) predict(outcome(1))
marginsplot, recast(line) recastci(rarea) xtitle(" " "Perceived ideological distance") ///
	ytitle("Predicted probability of hardly ever trusting Congress" " ") ylabel(0(.1)1) ///
	title("Senior Senator") plotopts(lw(.7)) ysize(4) xsize(4)

meglm trustcongress2 seniddiff2 i.pid3##c.sen2dem female age nonwhite interest ///
	education2 nateconomy [pweight=weights] || statdist:, family(ordinal)
estat ic
margins, at(seniddiff2=(0(1)6)) predict(outcome(1))
marginsplot, recast(line) recastci(rarea) xtitle(" " "Perceived ideological distance") ///
	ytitle("Predicted probability of hardly ever trusting Congress" " ") ylabel(0(.1)1) ///
	title("Junior Senator") plotopts(lw(.7)) ysize(4) xsize(4)


* 2016 CCES appendix material	

* Placebo test

meglm trustprez hiddiff1 i.pid3##c.housedem female age nonwhite interest ///
	education2 nateconomy [pweight=weights] || statdist:, family(ordinal)
estat ic

meglm trustprez seniddiff1 i.pid3##c.sen1dem female age nonwhite interest ///
	education2 nateconomy [pweight=weights] || statdist:, family(ordinal)
estat ic

meglm trustprez seniddiff2 i.pid3##c.sen2dem female age nonwhite interest ///
	education2 nateconomy [pweight=weights] || statdist:, family(ordinal)
estat ic
	
	
* Copartisans and opposing partisans

* Copartisans
meglm trustcongress2 hiddiff1 i.pid3 female age nonwhite interest education2 nateconomy ///
	if (housedem==0 & pid3==3) | (housedem==1 & pid3==1) [pweight=weights] || statdist:, family(ordinal)
estat ic
	
meglm trustcongress2 seniddiff1 i.pid3 female age nonwhite interest education2 nateconomy ///
	if (sen1dem==0 & pid3==3) | (sen1dem==1 & pid3==1) [pweight=weights] || statdist:, family(ordinal)
estat ic

meglm trustcongress2 seniddiff2 i.pid3 female age nonwhite interest education2 nateconomy ///
	if (sen2dem==0 & pid3==3) | (sen2dem==1 & pid3==1) [pweight=weights] || statdist:, family(ordinal)
estat ic

* Opposing partisans
meglm trustcongress2 hiddiff1 i.pid3 female age nonwhite interest education2 nateconomy ///
	if (housedem==0 & pid3==1) | (housedem==1 & pid3==3) [pweight=weights] || statdist:, family(ordinal)
estat ic
	
meglm trustcongress2 seniddiff1 i.pid3 female age nonwhite interest education2 nateconomy ///
	if (sen1dem==0 & pid3==1) | (sen1dem==1 & pid3==3) [pweight=weights] || statdist:, family(ordinal)
estat ic
	
meglm trustcongress2 seniddiff2 i.pid3 female age nonwhite interest education2 nateconomy ///
	if (sen2dem==0 & pid3==1) | (sen2dem==1 & pid3==3) [pweight=weights] || statdist:, family(ordinal)
estat ic


**************
* Experiment *
**************

use "experiment replication.dta", clear

* Results in the main text of the paper and Model 1 in the appendix
reg trust2 ideodist
margins, at(ideodist=(0 1 2))
marginsplot, x(ideodist) recast(bar) ciopts(lc("black") lw(0.4)) ///
	level(95) title("") xtitle(" " "Ideological distance from State Senator") xlabel(0(1)2) ///
	ytitle("Predicted value of trust" " ") ysize(4) xsize(4) ylabel(0(.2)1.6) ///
	ci1opts(lc("black") msize(*5) lw(.5)) plot1opts(col(gs4) barwidth(.9))

* Appendix model 2
reg trust2 ideodist pidsame

* Appendix model 3
reg trust2 c.ideodist##i.pidsame

	
